﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EvidenceLib.System;
using EvidenceService.Core;
using EvidenceService.System;
using EvidenceWeb.Models;
using EvidenceWeb.Util;
using NHibernate;

namespace EvidenceWeb.Controllers
{
    public class ReportController : Controller
    {
        //
        // GET: /Report/

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult EvidenceDeadlineReportView()
        {
            return View();
        }
        #region 超期未入库
        public ActionResult LoadEvidenceDeadlineReport()
        {
            using (ISession session = HbmSessionFactory.GetSession())
            {
                IList<object> ls = new List<object>();
                IDataReader reader = null;
                try
                {
                    int pageIndex = 0;
                    int.TryParse(Request["page"], out pageIndex);

                    int pageSize = 0;
                    int.TryParse(Request["rows"], out pageSize);

                    int totalRow = 0;

                    RoleService roleSvr = new RoleService(session);

                    User user = this.Session["User"] as User;


                    string json = Request["evidenceDeadlineReportQuery"];



                    int deadline = session.CreateSQLQuery("select cast(Value as int) from Ev_SystemParameter where FKey ='InStockDeadline'").UniqueResult<int>();

                    EvidenceDeadLineReportQuery query = JsonSerializerUtil.JsonToEntity<EvidenceDeadLineReportQuery>(json);

                    SqlCommand cmd = session.Connection.CreateCommand() as SqlCommand;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Ev_EvidenceDeadlineReport  "; //@Deadline,@TransferDeptId,@ManageDeptId,@PageIndex,@PageSize,@TotalRow

                    SqlParameter p0 = cmd.CreateParameter();
                    p0.ParameterName = "@ModelType";
                    p0.DbType = DbType.Int32;
                    p0.Value = query.ModelType;
                    cmd.Parameters.Add(p0);

                    SqlParameter p1 = cmd.CreateParameter();
                    p1.ParameterName = "@Deadline";
                    p1.DbType = DbType.Int32;
                    p1.Value = query.Deadline;
                    cmd.Parameters.Add(p1);

                    SqlParameter p11 = cmd.CreateParameter();
                    p11.ParameterName = "@CloseToExpire";
                    p11.DbType = DbType.Int32;
                    p11.Value = query.CloseToExpire;
                    cmd.Parameters.Add(p11);

                    SqlParameter p2 = cmd.CreateParameter();
                    p2.ParameterName = "@TransferDeptId";
                    p2.DbType = DbType.Int32;
                    p2.Value = query.TransferDeptId;
                    cmd.Parameters.Add(p2);

                    SqlParameter p3 = cmd.CreateParameter();
                    p3.ParameterName = "@ManageDeptId";
                    p3.DbType = DbType.Int32;
                    p3.Value = query.ManageDeptId;
                    cmd.Parameters.Add(p3);

                    SqlParameter p4 = cmd.CreateParameter();
                    p4.ParameterName = "@PageIndex";
                    p4.DbType = DbType.Int32;
                    p4.Value = pageIndex;
                    cmd.Parameters.Add(p4);


                    SqlParameter p5 = cmd.CreateParameter();
                    p5.ParameterName = "@PageSize";
                    p5.DbType = DbType.Int32;
                    p5.Value = pageSize;
                    cmd.Parameters.Add(p5);

                    SqlParameter p51 = cmd.CreateParameter();
                    p51.ParameterName = "@UserId";
                    p51.DbType = DbType.Int32;
                    p51.Value = user.Id;
                    cmd.Parameters.Add(p51);

                    SqlParameter p6 = cmd.CreateParameter();
                    p6.ParameterName = "@TotalRow";
                    p6.DbType = DbType.Int32;
                    p6.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(p6);

                    reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {

                        var row = new
                        {
                            CaseId = Convert.ToInt32(reader["CaseId"]),
                            CaseNo = Convert.ToString(reader["CaseNo"]),
                            CaseTypeName = Convert.ToString(reader["CaseTypeName"]),
                            EvidenceId = Convert.ToInt32(reader["EvidenceId"]),
                            EvidenceTypeName = Convert.ToString(reader["EvidenceTypeName"]),
                            EvidenceName = Convert.ToString(reader["EvidenceName"]),
                            EvidenceModel = Convert.ToString(reader["EvidenceModel"]),
                            EQty = Convert.ToDouble(reader["EQty"]),
                            EStockQty = Convert.ToDouble(reader["EStockQty"]),
                            EOutStockQty = Convert.ToDouble(reader["EOutStockQty"]),
                            EInquiryOutStockQty = Convert.ToDouble(reader["EInquiryOutStockQty"]),
                            TDeptName = Convert.ToString(reader["TDeptName"]),
                            MDeptName = Convert.ToString(reader["MDeptName"]),
                            TName = Convert.ToString(reader["TName"]),
                            UMName = Convert.ToString(reader["UMName"]),
                            OverHour = Convert.ToInt32(reader["OverHour"]),
                            MindHour =Convert.ToInt32(reader["MindHour"]),
                            CheckTime = Convert.ToDateTime(reader["CheckTime"])
                        };

                        ls.Add(row);

                    }

                    reader.Close();
                    reader.Dispose();

                    totalRow = p6.Value == null ? 0 : (int)p6.Value;

                    return Json(new { total = totalRow, rows = ls });

                }
                catch (Exception ex)
                {
                    if (reader != null && !reader.IsClosed)
                    {
                        reader.Close();
                        reader.Dispose();
                    }
                    return Json(new { total = 0, rows = ls, Message = ex.Message });
                }
            }
        }

        public ActionResult EvidenceOverDueSeizureReportView()
        {
            return View();
        }

        #endregion

        #region 超期未出库
        public ActionResult LoadEvidenceOverDueSeizureReport()
        {
            using (ISession session = HbmSessionFactory.GetSession())
            {
                IList<object> ls = new List<object>();
                IDataReader reader = null;
                try
                {
                    int pageIndex = 0;
                    int.TryParse(Request["page"], out pageIndex);

                    int pageSize = 0;
                    int.TryParse(Request["rows"], out pageSize);

                    int totalRow = 0;

                    RoleService roleSvr = new RoleService(session);

                    User user = this.Session["User"] as User;

                    string json = Request["evidenceOverDueSeizureReportQuery"];

                    //int mindDay = session.CreateSQLQuery("select cast(Value as int) from Ev_SystemParameter where FKey ='OutStockMind'").UniqueResult<int>();

                    EvidenceOverDueSeizureReportQuery query = JsonSerializerUtil.JsonToEntity<EvidenceOverDueSeizureReportQuery>(json);

                    SqlCommand cmd = session.Connection.CreateCommand() as SqlCommand;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "Ev_EvidenceOverDueSeizureReport  "; //@Deadline,@TransferDeptId,@ManageDeptId,@PageIndex,@PageSize,@TotalRow

                    SqlParameter p0 = cmd.CreateParameter();
                    p0.ParameterName = "@ModelType";
                    p0.DbType = DbType.Int32;
                    p0.Value = query.ModelType;
                    cmd.Parameters.Add(p0);

                    //SqlParameter p1 = cmd.CreateParameter();
                    //p1.ParameterName = "@Deadline";
                    //p1.DbType = DbType.Int32;
                    //p1.Value = query.Deadline;
                    //cmd.Parameters.Add(p1);

                    SqlParameter p11 = cmd.CreateParameter();
                    p11.ParameterName = "@CloseToExpire";
                    p11.DbType = DbType.Int32;
                    p11.Value = query.CloseToExpire;
                    cmd.Parameters.Add(p11);

                    SqlParameter p2 = cmd.CreateParameter();
                    p2.ParameterName = "@TransferDeptId";
                    p2.DbType = DbType.Int32;
                    p2.Value = query.TransferDeptId;
                    cmd.Parameters.Add(p2);

                    SqlParameter p3 = cmd.CreateParameter();
                    p3.ParameterName = "@ManageDeptId";
                    p3.DbType = DbType.Int32;
                    p3.Value = query.ManageDeptId;
                    cmd.Parameters.Add(p3);

                    SqlParameter p4 = cmd.CreateParameter();
                    p4.ParameterName = "@PageIndex";
                    p4.DbType = DbType.Int32;
                    p4.Value = pageIndex;
                    cmd.Parameters.Add(p4);


                    SqlParameter p5 = cmd.CreateParameter();
                    p5.ParameterName = "@PageSize";
                    p5.DbType = DbType.Int32;
                    p5.Value = pageSize;
                    cmd.Parameters.Add(p5);

                    SqlParameter p51 = cmd.CreateParameter();
                    p51.ParameterName = "@UserId";
                    p51.DbType = DbType.Int32;
                    p51.Value = user.Id;
                    cmd.Parameters.Add(p51);

                    SqlParameter p6 = cmd.CreateParameter();
                    p6.ParameterName = "@TotalRow";
                    p6.DbType = DbType.Int32;
                    p6.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(p6);

                    reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {

                        var row = new
                        {
                            CaseId = Convert.ToInt32(reader["CaseId"]),
                            CaseNo = Convert.ToString(reader["CaseNo"]),
                            CaseTypeName = Convert.ToString(reader["CaseTypeName"]),
                            EvidenceId = Convert.ToInt32(reader["EvidenceId"]),
                            EvidenceTypeName = Convert.ToString(reader["EvidenceTypeName"]),
                            EvidenceName = Convert.ToString(reader["EvidenceName"]),
                            EvidenceModel = Convert.ToString(reader["EvidenceModel"]),
                            EQty = Convert.ToDouble(reader["EQty"]),
                            EStockQty = Convert.ToDouble(reader["EStockQty"]),
                            EOutStockQty = Convert.ToDouble(reader["EOutStockQty"]),
                            EInquiryOutStockQty = Convert.ToDouble(reader["EInquiryOutStockQty"]),
                            TDeptName = Convert.ToString(reader["TDeptName"]),
                            MDeptName = Convert.ToString(reader["MDeptName"]),
                            TName = Convert.ToString(reader["TName"]),
                            UMName = Convert.ToString(reader["UMName"]),
                            OverDay = Convert.ToInt32(reader["OverDay"]),
                            MindDay = Convert.ToInt32(reader["MindDay"]),
                            CheckTime = Convert.ToDateTime(reader["CheckTime"]),
                            SeizureDay = Convert.ToInt32(reader["SeizureDay"])
                        };

                        ls.Add(row);

                    }

                    reader.Close();
                    reader.Dispose();

                    totalRow = p6.Value == null ? 0 : (int)p6.Value;

                    return Json(new { total = totalRow, rows = ls });

                }
                catch (Exception ex)
                {
                    if (reader != null && !reader.IsClosed)
                    {
                        reader.Close();
                        reader.Dispose();
                    }
                    return Json(new { total = 0, rows = ls, Message = ex.Message });
                }
            }
        }

        #endregion 
    }
}
